package ca.abubakrsagar.breadboardsim;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

/**
 * The switch component. Used to control if the current flows through that area
 * or not.
 * 
 * @author Sagar, Abubakr
 * @since May 29, 2012
 * 
 */
public class Switch extends Component {

	private boolean isOn; // Boolean value to keep track of the state of the
							// switch.

	/**
	 * Creates a new switch component with the specified state.
	 * 
	 * @param isOn
	 *            The state of the switch.
	 */
	public Switch(boolean isOn) {
		this.isOn = isOn;
		this.setPreferredSize(new Dimension(26, 20));
		this.setVisible(true);
		setComponentType(2);
	}

	/**
	 * Draws the switch based on its state (on or off).
	 */
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		if (isOn) {
			g2.setColor(Color.green);
			g2.fillRect(0, 0, 17, 7);
			g2.setColor(Color.black);
			g2.fillRect(8, 1, 5, 5);
		} else {
			g2.setColor(Color.red);
			g2.fillRect(0, 0, 17, 7);
			g2.setColor(Color.black);
			g2.fillRect(2, 1, 5, 5);
		}

	}

	/**
	 * @return The state of the switch.
	 */
	public boolean isOn() {
		return isOn;
	}

	/**
	 * Sets the state of the switch to the specified boolean value.
	 * 
	 * @param isOn
	 *            The state of the switch.
	 */
	public void setOn(boolean isOn) {
		this.isOn = isOn;
	}
}
